-- This is no gadget but a class file!
-- An object of this class represents a collection of engines in the view of the game.
-- You may add engines, search engines by special properties
-- and change engines properties if a unitID is changing for example.
--
-- Example code how to use this class:
--
-- include("LuaRules/Classes/OOLuaEngine.lua") 				## this are the includes which you need
-- include("LuaRules/Classes/OOLuaEngineCollection.lua")
--
-- local e1 = Engine:new(10,21)    						 	## create an engine object (UnitID: 10, UnitDefID: 21)
-- local e2 = Engine:new(20,22)	    			     		## create a second engine object
-- local collection = EngineCollection:new()				## create an engine collection object
-- collection:AddEngine(e1)									## add first engine to the collection
-- collection:AddEngine(e2)									## add second engine to the collection
-- local e = collection:GetEngineByUnitID(20)				## search an engine in the collection by its unitID

include("LuaRules/Classes/OOLuaClass.lua")
include("LuaRules/Classes/OOMetaEngine.lua")

EngineCollection = {}
local EngineCollection_mt = Class(EngineCollection)

--Constructor
function EngineCollection:new()
  return setmetatable( 	{--declare member variables at this place:
  					 	Engines = {}
  					 	}, EngineCollection_mt)
end

--Adds an engine to the collection
--param e: the engine which will be added
function EngineCollection:AddEngine(e)
	local i = 0
	while (self.Engines[i] ~= nil) do	-- could we not just get the table length vie "#" ? I guess a question is whether you want to refill gaps that occur from Engines that might be removed from the collection.
		i = i+1
	end
	self.Engines[i] = e
end

--Searches an engine in the collection by its unitID.
--return: 	Returns the first engine, which is found. Returns nil, if no engine with this unitID was found.
function EngineCollection:GetEngineByUnitID(unitID)
	local i = 0
	while (self.Engines[i] ~= nil) do
		if (self.Engines[i]:GetUnitID() == unitID) then
			return self.Engines[i]
		end
		i = i + 1
	end
	return nil
end

--Searches an engine in the collection by its teamID.
--return: 	Returns the the first engine, which is found. Returns nil, if no engine with this teamID was found.
function EngineCollection:GetEngineByTeamID(teamID)		-- not sure if this is useful in this form - would need to change output to a table of all Engines belonging to this teamID
	local i = 0																																			--> easy enough to do :)				
	while (self.Engines[i] ~= nil) do
		if (self.Engines[i]:GetTeamID() == teamID) then
			return self.Engines[i]
		end
		i = i + 1
	end
	return nil
end

--Searches an engine in the collection by its playerID.
--return: 	Returns the the first engine, which is found. Returns nil, if no engine with this playerID was found.
function EngineCollection:GetEngineByPlayerID(playerID)		-- see above. Though using the playerID may be something special we want to use, so not to worry about
	local i = 0
	while (self.Engines[i] ~= nil) do
		if (self.Engines[i]:GetPlayerID() == playerID) then
			return self.Engines[i]
		end
		i = i + 1
	end
	return nil
end